import {defineStore} from "pinia";

export const useViewTagsStore = defineStore('viewTags', {
    state: () => ({
        viewTags: [],
        keepLiveRoute: [],
        routeKey: null,
        routeShow: true
    }),
    actions: {
        pushViewTags(route: any) {
            let target = this.viewTags.find((item: any) => item.fullPath === route.fullPath)
            let isName = route.name
            if (!target && isName) {
                // @ts-ignore
                this.viewTags.push(route)
            }
        },
        removeViewTags(route: any) {
            this.viewTags.forEach((item: any, index: any) => {
                if (item.fullPath === route.fullPath) {
                    this.viewTags.splice(index, 1)
                }
            })
        },
        updateViewTags(route: any) {
            this.viewTags.forEach((item: any) => {
                if (item.fullPath == route.fullPath) {
                    item = Object.assign(item, route)
                }
            })
        },
        updateViewTagsTitle(title = '') {
            const nowFullPath = location.hash.substring(1)
            this.viewTags.forEach((item: any) => {
                if (item.fullPath == nowFullPath) {
                    item.meta.title = title
                }
            })
        },
        clearViewTags() {
            this.viewTags = []
        },

        pushKeepLive(component: any) {
            // @ts-ignore
            if (!this.keepLiveRoute.includes(component)) {
                // @ts-ignore
                this.keepLiveRoute.push(component)
            }
        },
        removeKeepLive(component: any) {
            // @ts-ignore
            var index = this.keepLiveRoute.indexOf(component);
            if (index !== -1) {
                this.keepLiveRoute.splice(index, 1);
            }
        },
        clearKeepLive() {
            this.keepLiveRoute = []
        },
        setRouteKey(key: any) {
            this.routeKey = key
        },
        setRouteShow(key: any) {
            this.routeShow = key
        }
    }
})
